/**
 * Created with JetBrains WebStorm.
 * User: Mikhail Kavaliou
 * Date: 12.10.13
 * Email: mikhail.kavaliou@gmail.com
 */

$(document).ready(function() {
    setCommonPageMetrics();

    // #content scrollbars styling
    fleXenv.fleXcrollMain("content");
});

$(window).resize(function() {
    setCommonPageMetrics();
});

var setCommonPageMetrics = function() {
    setBodyHeight();
    setContentHeight();
    setLeftPanelHeight();
};

var setBodyHeight = function() {
    var bodyHeight = 0;
    if (typeof( window.innerWidth ) == 'number') {
        //Non-IE
        bodyHeight = window.innerHeight;
    } else if (document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight )) {
        //IE 6+ in 'standards compliant mode'
        bodyHeight = document.documentElement.clientHeight;
    } else if (document.body && ( document.body.clientWidth || document.body.clientHeight )) {
        //IE 4 compatible
        bodyHeight = document.body.clientHeight;
    }

    // subtracting border height and #content padding
    var mainContainer = $('#main_container');
    var content = $('#content');
    bodyHeight -= mainContainer.margin().top + mainContainer.margin().bottom +
        content.padding().top + content.padding().bottom;

    $('body').height(bodyHeight);
};

var setContentHeight = function() {
    var headerHeight = $('#_header').height();
    var bodyHeight = $('body').height();
    var contentHeight = bodyHeight - headerHeight;
    $('#content').height(contentHeight);
};

var setLeftPanelHeight = function() {
    var contentHeight = $('#content').height();
    $('#_leftpanel').height(contentHeight);
};

var montageImages = function() {
    var $container = $('#am-container'),
        $imgs = $container.find('img').hide(),
        totalImgs = $imgs.length,
        cnt = 0;

    $imgs.each(function(i) {
        var $img = $(this);
        $('<img/>').load(function() {
            ++cnt;
            if (cnt === totalImgs) {
                $imgs.show();
                $container.montage({
                    fillLastRow: true,
                    alternateHeight: true,
                    alternateHeightRange: {
                        min: 80,
                        max: 180
                    }
                });
            }
        }).attr('src', $img.attr('src'));
    });
};
